	<?php

	/**
 * Controller_finance
 */

	class Controller_finance extends MyApp {

		public function __construct()
		{

			parent::__construct();

			$this->model->user    		= YOU::loadModel('user');
			$this->model->staff   		= YOU::loadModel('staff');
			$this->model->file    		= YOU::loadModel('file');
			$this->model->semester    	= YOU::loadModel('semester');
			$this->model->finance    	= YOU::loadModel('finance');

		}
		/** 首页 **/
		public function actionIndex()
		{

			$this->tpl->display();
		}
		/** 财务列表 **/
		public function actionFinanceList()
		{
			// 分页
			YOU::loadLib('page', "", '0');
			$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_finance`");
			$count = $tmp['count'];
			$page_total = ceil($count/PAGE_LIMIT);
			// 当前页
			$p = intval($_GET['p']);
			if ($p == 0){
				$p  = 1;
			}
			if ($p > 1){
				$start = PAGE_LIMIT * ($p-1);
			}else{
				$start = 0;
			}

			$finance_lists = $this->db->fetchAll("SELECT * FROM `tbl_finance` ORDER BY finance_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

			$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
			$page->next_page   = '下一页>';
			$page->pre_page    = '<上一页';
			$page->style_path  = "static/page_style";

			$this->tpl->assign("page", $page->show(4, "yahoo"));
			$this->tpl->assign("finance_lists", $finance_lists);
			$this->tpl->display('finance/m.finance.list.tpl.php');
		}
		/** 财务添加 **/
		public function actionFinanceAdd()
		{
			if ($_POST){

				$finance_name     		= $this->post->finance_name;

				// 写入数据库
				$info = array(

				"finance_name"      	=> $finance_name,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);
				$this->db->do_insert("tbl_finance", $info);

				// 跳转
				Helper_Client::location("添加成功", "index.php?a=finance.financelist");
			}

			$this->tpl->display('finance/finance.add.tpl.php');
		}
		/** 财务修改 **/
		public function actionFinanceUpdate()
		{
			$finance_id   = (int)$this->get->finance_id;
			$finance_data = $this->db->fetchRow("SELECT * FROM `tbl_finance` WHERE `finance_id`='{$finance_id}'");

			if (empty($finance_data)){
				Helper_Client::alert("财务名称不存在");
			}

			if ($_POST){

				$finance_name     	    = $this->post->finance_name;

				// 写入数据库
				$info = array(

				"finance_name"      	=> $finance_name,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",
				);
				$this->db->do_update("tbl_finance", $info, "finance_id='{$finance_id}'");

				// 跳转
				Helper_Client::location("修改成功", "index.php?a=finance.financeList");
			}

			$this->tpl->assign("finance_data", $finance_data);
			$this->tpl->display('finance/finance.update.tpl.php');
		}
		/** 财务删除 **/
		public function actionFinanceDel()
		{
			$finance_id = $this->get->finance_id;
			// 判断财务表下面是否有内容
			$row = $this->model->finance->getContInfo($finance_id);
			if (!empty($row)) {
				Helper_Client::alert("请先全部删除报表内容");
			}
			$this->db->query("DELETE  FROM `tbl_finance` WHERE finance_id='{$finance_id}'");

			// 跳转
			Helper_Client::location("删除成功", "index.php?a=finance.financeList");
		}
		/** --------------------------------- 财务报表 --------------------------------- **/
		/** 财务报表列表 **/
		public function actionFinanceTableList()
		{
			// 获取学期
			$semester_lists = $this->db->fetchAll("SELECT * FROM `tbl_semester`");
			foreach ($semester_lists as $key => $value){
				$semester_lists[$key]['semester_name'] = $this->semester_name[$value['semester_name']];

			}
		
			// 搜索
			$semester_id     	 = $this->get->semester_id;
			$finance_id     	 = $this->get->finance_id;

			$where =' ';

			if (!empty($semester_id)){
				$where = " AND a.semester_id ={$semester_id} ";
				$this->tpl->assign("semester_id", $semester_id);
			}	
			
			// 分页
			YOU::loadLib('page', "", '0');
			$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_financetable` AS a,`tbl_finance` AS b WHERE a.finance_id=b.finance_id AND a.finance_id ={$finance_id} {$where}");
			$count = $tmp['count'];
			$page_total = ceil($count/PAGE_LIMIT);
			// 当前页
			$p = intval($_GET['p']);
			if ($p == 0){
				$p  = 1;
			}
			if ($p > 1){
				$start = PAGE_LIMIT * ($p-1);
			}else{
				$start = 0;
			}

			$financetable_lists = $this->db->fetchAll("SELECT a.*  FROM `tbl_financetable` AS a,`tbl_finance` AS b WHERE a.finance_id=b.finance_id AND a.finance_id ={$finance_id} {$where} ORDER BY a.financetable_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
			
			$create_time = date("Y-m-d",time($financetable_lists['create_time']));
			foreach ($financetable_lists as $key=> $value){
				// 提交人
				$financetable_lists[$key]['staff_name']  = $this->model->user->getNameById($value['user_id']);
				
				// 学年学期
				$row = $this->model->semester->getInfo($value['semester_id']);
				$financetable_lists[$key]['semester_name'] = $row['semester_name'];
			}


			$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
			$page->next_page   = '下一页>';
			$page->pre_page    = '<上一页';
			$page->style_path  = "static/page_style";
			$this->tpl->assign("page", $page->show(4, "yahoo"));
			
			$this->tpl->assign("semester_lists", $semester_lists);
			$this->tpl->assign("financetable_lists", $financetable_lists);
			$this->tpl->assign("create_time", $create_time);
			$this->tpl->assign("finance_id", $finance_id);

			$this->tpl->display('finance/m.financeteble.list.tpl.php');
		}
		/** 财务报表添加 **/
		public function actionFinancetableAdd()
		{	
			$finance_id     		= $this->get->finance_id;	
			// 获取学期
			$semester_lists = $this->db->fetchAll("SELECT * FROM `tbl_semester`");
			// 学年名称
			foreach ($semester_lists as $key=>$value){
				$semester_lists[$key]['semester_name'] = $this->semester_name[$value['semester_name']];
			}
			
			if ($_POST){

				$financetable_title     = $this->post->financetable_title;
				$semester_id     		= $this->post->semester_id;

				// 写入数据库
				$info = array(

				"financetable_title"   	=> $financetable_title,
				"semester_id"      		=> $semester_id,
				"finance_id"      		=> $finance_id,
				"user_id"      			=> $this->user['user_id'],
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);
				$this->db->do_insert("tbl_financetable", $info);
				$financetable_id = $this->db->insert_id();

				// 附件上传
				$file = $this->model->file->upload("financetable_file", $financetable_id, array("txt","ppt", "xls", "xlsx", "docs", "docx", "doc", "pdf", "jpg", "png", "pdf", "gif", "zip", "rar","mp4","bov","wmv","dvd","rmvb","avi","rm",));

				if (!empty($file)){
					$info = array(
					"financetable_file" => $file['fname'],
					);

					$this->db->do_update("tbl_financetable", $info, "financetable_id='{$financetable_id}'");
				}
				// 跳转
				Helper_Client::location("添加成功", "index.php?a=finance.financetableList&finance_id={$finance_id}");
			}

			$this->tpl->assign("semester_lists", $semester_lists);
			$this->tpl->display('finance/financeteble.add.tpl.php');
		}
		/** 财务报表修改 **/
		public function actionFinancetableUpdate()
		{

			$financetable_id   = (int)$this->get->financetable_id;
			$financetable_data = $this->db->fetchRow("SELECT * FROM `tbl_financetable` WHERE `financetable_id`='{$financetable_id}'");

			// 获取学期
			$semester_lists = $this->db->fetchAll("SELECT * FROM `tbl_semester`");

			if (empty($financetable_data)){
				Helper_Client::alert("财务报表不存在");
			}

			if ($_POST){

				$financetable_title     = $this->post->financetable_title;
				$semester_id     		= $this->post->semester_id;

				// 写入数据库
				$info = array(

				"financetable_title"   	=> $financetable_title,
				"semester_id"      		=> $semester_id,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",
				);
				$this->db->do_update("tbl_financetable", $info, "financetable_id='{$financetable_id}'");
				
				// 附件上传
			if ($this->post->financetable_file_re=='1'){
				// 附件上传
				$file = $this->model->file->upload("financetable_file", $financetable_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

				if (!empty($file)){
					$info = array(
					"financetable_file" => $file['fname'],
					);

					$this->db->do_update("tbl_financetable", $info, "financetable_id='{$financetable_id}'");
				}
			}

				// 跳转
				Helper_Client::location("修改成功", "index.php?a=finance.financetableList");
			}

			$this->tpl->assign("financetable_data", $financetable_data);
			$this->tpl->assign("semester_lists", $semester_lists);
			$this->tpl->display('finance/financeteble.update.tpl.php');
		}
		/** 财务报表删除 **/
		public function actionFinancetableDel()
		{
			$financetable_id = $this->get->financetable_id;
			$this->db->query("DELETE  FROM `tbl_financetable` WHERE financetable_id='{$financetable_id}'");

			// 跳转
			Helper_Client::location("删除成功", "index.php?a=finance.financetableList");
		}
		/** 财务报表详情 **/
		public function actionFinancetableDetails()
		{
			$financetable_id = $this->get->financetable_id;
			// 获取信息
			$financetable_data = $this->db->fetchRow("SELECT * FROM `tbl_financetable` WHERE `financetable_id`='{$financetable_id}'");
			// 学期
			$row = $this->model->semester->getInfo($financetable_data['semester_id']);
			$financetable_data['semester_xname'] = $row['semester_xname'];
			$financetable_data['semester_name']  = $row['semester_name'];

			$this->tpl->assign("financetable_data", $financetable_data);
			$this->tpl->display('finance/m.financeteble.details.tpl.php');
		}
		/** 下载**/
		public function actionoutput(){
			$fname = $this->get->financetable_file;
			$fpath = UPLOAD_PATH."/financetable_file/{$fname}";
	
			// 输出下载文件头s
			header("Content-Disposition: attachment; filename={$fname}");
			// 在线输出
			readfile($fpath);

		}


	}

?>